package com.jiazhong.ssm.test01整合.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.ssm.test01整合.mapper.ScheduleMapper;
import com.jiazhong.ssm.test01整合.pojo.Schedule;
import com.jiazhong.ssm.test01整合.service.ScheduleService;
import com.jiazhong.ssm.test01整合.utils.PageBean;
import com.jiazhong.ssm.test01整合.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:shuXin
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public R page(int pageSize, int currentPage) {
        //分页
        PageHelper.startPage(currentPage, pageSize);
        //查询
        List<Schedule> schedules = scheduleMapper.queryList();
        //分页数据进行装配
        PageInfo<Schedule> info = new PageInfo<>(schedules);
        //封装数据至返回的结果
        PageBean<Schedule> pageBean = new PageBean<>(currentPage, pageSize, info.getTotal(), info.getList());
        //返回结果
        R r = R.ok(pageBean);
        return r;


    }

    @Override
    public R save(Schedule schedule) {
        int insert = scheduleMapper.insert(schedule);
        //进行判断
        if (insert > 0) {
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }

    @Override
    public R delete(int id) {
        int delete = scheduleMapper.delete(id);
        if (delete > 0) {
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R update(Schedule schedule) {
        //判断id是否为空
        if (schedule.getId() == null) {
            return R.fail("id为空，不能查询");
        }
        int update = scheduleMapper.update(schedule);
        if (update > 0) {
            return R.ok(null);
        }
        return R.fail(null);
    }
}
